package Dungeon;

import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.Set;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import SquarePackage.Square;

public class DungeonTest {
	
	private Dungeon c ;
	private Dungeon v ;
	private Square st;
	private Square st2;
	private Square st3;
	private static Dimension d;
	private static Dimension d2;
	private static Dimension d3;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		d = new Dimension(2,3,1);
		d2 = new Dimension(2,3,2);
		d3 = new Dimension(3,3,1);
	}
	
	@Before
	public void setUp() throws Exception {
		st = new Square();
		st2 = new Square();
		st3 = new Square();
		c = new Dungeon();
		v = new Dungeon();
	}

	@Test
	public void testDungeonLongLongLong() {
		Dungeon lll = new Dungeon(15,15,15);
		assertEquals(new Dimension(15,15,15),lll.getMaxDimension());
	}

	@Test(expected = IllegalArgumentException.class)
	public void testDungeonLongLongLongNegative() {
		Dungeon wrong = new Dungeon(-5,-5,-5);
		fail("one of the given coordinate values is not valid.");
	}
	
	@Test
	public void testDungeonDimension() {
		Dungeon dim = new Dungeon(new Dimension(15,15,15));
		assertEquals(new Dimension(15,15,15),dim.getMaxDimension());
	}

	@Test
	public void testDungeon() {
		assertEquals(new Dimension(20,20,20),c.getMaxDimension());
	}

	@Test
	public void testRemoveRoom() {
		v.addSquare(st, d);
		Room room = new Room(v, st, d);
		Set<Room> list1 = new HashSet<Room>();
		list1.add(room);
		assertEquals(room,v.getRoomOf(d));
		assertEquals(list1,v.getRooms());
		v.addSquare(st2, d2);
		Room room2 = new Room(v, st2, d2);
		list1.add(room2);
		assertEquals(list1,v.getRooms());
		v.removeRoom(v.getRoomOf(d2));
		list1.remove(room2);
		assertEquals(list1,v.getRooms());
	}
	
	@Test
	public void testSetMaxDimension() {
		v.setMaxDimension(new Dimension(80,80,80));
		assertEquals(new Dimension(80,80,80),v.getMaxDimension());
	}
	
	@Test(expected = IllegalArgumentException.class)
	public void testSetMaxDimensionLower() {
		v.setMaxDimension(new Dimension(5,5,5));
		assertEquals(new Dimension(15,15,15),v.getMaxDimension());
	}

	@Test
	public void testGetRooms() {
		v.addSquare(st, d);
		Room room = new Room(v, st, d);
		HashSet<Room> list = new HashSet<Room>();
		list.add(room);
		assertEquals(list,v.getRooms());
		v.addSquare(st2, d2);
		Room room2 = new Room(v, st2, d2);
		list.add(room2);
		assertEquals(list,v.getRooms());
	}

	@Test
	public void testAddSquare() {
		v.addSquare(st, d);
		v.addSquare(st2, d2);
		assertTrue(v.getAllSquares().contains(st));
		assertTrue(v.getAllSquares().contains(st2));
		assertTrue(v.getUsedDimensions().contains(d));
		assertTrue(v.getUsedDimensions().contains(d2));
		assertEquals(2,v.getRooms().size());
	}

	@Test
	public void testGetSquareAt() {
		v.addSquare(st, d);
		assertEquals(st,v.getSquareAt(d));
		assertEquals(st,v.getSquareAt(new Dimension(2,3,1)));
	}

	@Test
	public void testRemoveSquareAt() {
		v.addSquare(st,d);
		v.addSquare(st2, d2);
		v.removeSquareAt(d);
		assertTrue(st.isTerminated());
		assertFalse(v.getAllSquares().contains(st));
	}

	@Test
	public void testIsInSameRoomAs() {
		v.addSquare(st, d);
		v.addSquare(st3, d3);
		assertTrue(v.isInSameRoomAs(st, st3));
	}

	@Test
	public void testGetAllSquares() {
		v.addSquare(st, d);
		Set<Square> list = new HashSet<Square>();
		list.add(st);
		assertEquals(list,v.getAllSquares());
		v.addSquare(st2, d2);
		list.add(st2);
		assertEquals(list,v.getAllSquares());
	}

	@Test
	public void testGetUsedDimensions() {
		v.addSquare(st, d);
		HashSet<Dimension> dimensions = new HashSet<Dimension>();
		dimensions.add(d);
		HashSet<Dimension> dims = (HashSet<Dimension>) v.getUsedDimensions();
		assertEquals(dimensions, dims);
	}

	@Test
	public void testIsAvailable() {
		v.addSquare(st, d);
		assertFalse(v.isAvailable(d));
		assertTrue(v.isAvailable(d2));
	}

	@Test
	public void testGetRoomOfSquare() {
		v.addSquare(st, d);
		assertTrue(v.getRooms().contains(v.getRoomOf(st)));
		assertEquals(null,v.getRoomOf(st2));
	}

	@Test
	public void testGetRoomOfDimension() {
		v.addSquare(st, d);
		assertTrue(v.getRoomOf(d).getAllSquares().contains(st));
		assertEquals(null,v.getRoomOf(d2));
	}
	
}
